package Demo65;

/**
 * 最长公共前缀
 * https://leetcode.cn/submissions/detail/561078448/
 */
class Solution {
    // 两两比较
    public String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length == 0) {
            return "";
        }
        int length = strs[0].length();
        int count = strs.length;
        for(int i = 0; i < length;i++) {
            char tmp = strs[0].charAt(i);
            for(int j = 1; j < count; j++) {
                if(i == strs[j].length() || tmp != strs[j].charAt(i)) {
                    return strs[0].substring(0,i);
                }   
            }
        }
        return strs[0];
    }
    // 一列一列比较
    public String longestCommonPrefix1(String[] strs) {
        if(strs == null || strs.length == 0) {
            return "";
        }
        int length = strs[0].length();
        int count = strs.length;
        for(int i = 0; i < length;i++) {
            char tmp = strs[0].charAt(i);
            for(int j = 1; j < count; j++) {
                if(i == strs[j].length() || tmp != strs[j].charAt(i)) {
                    return strs[0].substring(0,i);
                }
            }
        }
        return strs[0];
    }
}
class Demo {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String ret = solution.longestCommonPrefix(new String[]{"flower", "flow", "flight"});
        System.out.println(ret);
    }
}
